'use strict';

var _interopRequireDefault = require('@babel/runtime/helpers/interopRequireDefault');
Object.defineProperty(exports, '__esModule', {
  value: true,
});
exports.fileValidatorFactory = exports.convetAccept = void 0;
var _logger = _interopRequireDefault(require('../../logger'));
// https://developer.mozilla.org/en-US/docs/Web/HTML/Element/input/file#Unique_file_type_specifiers
var convetAccept = function convetAccept(accept) {
  return (accept || '')
    .split(',')
    .map(function (type) {
      var convertedType = type;
      switch (type) {
        // 图片
        case 'image':
          convertedType = 'image/*';
          break;
        // 音频
        case 'audio':
          convertedType = 'audio/*';
          break;
        // 视频
        case 'vedio':
          convertedType = 'vedio/*';
          break;
        // doc 文档
        case 'doc':
          convertedType =
            '.doc,.docx,.xml,application/msword,application/vnd.openxmlformats-officedocument.wordprocessingml.document';
          break;
        // excel
        case 'excel':
          convertedType =
            '.xls,.xlsx,application/vnd.ms-excel,application/vnd.openxmlformats-officedocument.spreadsheetml.sheet';
          break;
        case 'pdf':
          convertedType = '.pdf';
          break;
        case 'ppt':
          convertedType =
            '.ppt,.pptx,application/vnd.ms-powerpoint,application/vnd.openxmlformats-officedocument.presentationml.presentation';
          break;
      }
      return convertedType;
    })
    .join(',');
};
// 校验文件
exports.convetAccept = convetAccept;
var fileValidatorFactory = function fileValidatorFactory(props) {
  return function (file) {
    var preventUpload = false;
    if (props.accept) {
      var typeAllowed = false;
      var hasAcceptNotRegistered = false;
      var message = '';
      props.accept
        .split(',')
        .map(function (type) {
          var matched;
          switch (type) {
            // 图片
            case 'image':
              matched = file.type.match(/^image\/\w*$/);
              message = '请上传图片';
              if (matched) typeAllowed = true;
              break;
            // 音频
            case 'audio':
              matched = file.type.match(/^audio\/\w*$/);
              message = '请上传音频';
              if (matched) typeAllowed = true;
              break;
            // 视频
            case 'vedio':
              matched = file.type.match(/^vedio\/\w*$/);
              message = '请上传视频';
              if (matched) typeAllowed = true;
              break;
            // doc 文档
            case 'doc':
              matched = file.type.match(
                /^(application\/msword)|(application\/vnd\.openxmlformats-officedocument\.wordprocessingml\.document)$/,
              );
              message = '请上传 doc 文档';
              if (matched) typeAllowed = true;
              break;
            // excel
            case 'excel':
              matched = file.type.match(
                /^(application\/vnd\.ms-excel)|(application\/vnd\.openxmlformats-officedocument\.spreadsheetml\.sheet)$/,
              );
              message = '请上传 excel';
              if (matched) typeAllowed = true;
              break;
            case 'pdf':
              matched = file.type.match(/^application\/pdf$/);
              message = '请上传 pdf';
              if (matched) typeAllowed = true;
              break;
            case 'ppt':
              matched = file.type.match(
                /^(application\/vnd\.ms-powerpoint)|(application\/vnd\.openxmlformats-officedocument\.presentationml\.presentation)$/,
              );
              message = '请上传 ppt';
              if (matched) typeAllowed = true;
              break;
            default:
              hasAcceptNotRegistered = true;
          }
        })
        .join(',');
      if (!hasAcceptNotRegistered && !typeAllowed) {
        preventUpload = true;
        _logger.default.fail('\u6587\u4EF6\u683C\u5F0F\u4E0D\u6B63\u786E');
        // @ts-ignore
        file.uploadPrevented = preventUpload;
        return !preventUpload;
      }
    }
    if (props.maxSize) {
      var lessThenMaxSize = file.size / 1024 / 1024 < props.maxSize;
      if (!lessThenMaxSize) {
        preventUpload = true;
        _logger.default.fail('\u6587\u4EF6\u4E0D\u80FD\u5927\u4E8E '.concat(props.maxSize, 'MB'));
        // @ts-ignore
        file.uploadPrevented = preventUpload;
        return !preventUpload;
      }
    }
    if (props.max) {
      if (props.fileList && props.fileList.length >= props.max) {
        preventUpload = true;
        _logger.default.fail(
          '\u4E0A\u4F20\u6587\u4EF6\u6570\u4E0D\u80FD\u8D85\u8FC7 '.concat(props.max, ' \u4E2A'),
        );
        // @ts-ignore
        file.uploadPrevented = preventUpload;
        return !preventUpload;
      }
    }
    // @ts-ignore
    file.uploadPrevented = preventUpload;
    return !preventUpload;
  };
};
exports.fileValidatorFactory = fileValidatorFactory;
